#include "InPerUartRead.h"

#define DEF_TAG             "UartRead"

Com_s32 g_InPer_UartRead_Head_Flag = 0;
Com_s32 g_InPer_UartRead_Len = 0;
Com_s32 g_InPer_UartRead_DataLen = DEF_HEAD_LENGTH;

void Ext_InPer_UartRead_Head_Flag(Com_s32 flag)
{
    g_InPer_UartRead_Head_Flag = flag;
}

void Ext_InPer_UartRead_Set_Len(Com_s32 len)
{
    g_InPer_UartRead_Len = len;
}

int Ext_InPer_UartRead_Process(Com_pu8 buff,Com_s32 serial_fd)
{
    int i;
    int retval;
    int nread = -1;
    fd_set rfds;
    struct timeval tv;
    static unsigned char rec_b[DEF_PER_UART_BUF_SIZE] = {0};
    unsigned char temp_r=0;

    tv.tv_sec = 0;			//set the rcv wait time
    tv.tv_usec = (DEF_PER_UART_READ_MS * 1000);	//100000us = 0.1s

    FD_ZERO(&rfds);
    FD_SET(serial_fd,&rfds);
    retval = select(serial_fd + 1, &rfds,NULL,NULL,&tv);
    if(retval == -1)
    {
        perror("select()");
    }
    else if(retval)
    {
        if (read(serial_fd, &temp_r, 1) > 0)
        {
            //PRT_NORMAL("0x%02x ",temp_r);
            if (g_InPer_UartRead_Head_Flag == 1)
            {
                rec_b[g_InPer_UartRead_Len] = temp_r;
                g_InPer_UartRead_Len++;
                if (g_InPer_UartRead_Len >= g_InPer_UartRead_DataLen)
                {
                    if (g_InPer_UartRead_Len == DEF_HEAD_LENGTH)
                    {
                        g_InPer_UartRead_DataLen = DEF_HEAD_LENGTH + ((rec_b[g_InPer_UartRead_Len - 2] << 8) + temp_r) + 1;
                        if (g_InPer_UartRead_DataLen > DEF_PER_UART_BUF_SIZE)
                        {
                            g_InPer_UartRead_Head_Flag = 0;
                            g_InPer_UartRead_Len = 0;
                            g_InPer_UartRead_DataLen = DEF_HEAD_LENGTH;
                            memset(rec_b, 0, sizeof(rec_b));
                            PRT_ERR(DEF_TAG,"receive error data length ----> \n");
                            usleep(10 * 1000);
                            return nread;
                        }
                    }
                    else
                    {
                        memcpy(buff, rec_b, g_InPer_UartRead_DataLen);
                        PRT_NORMAL("\nnread=%d ---> ", g_InPer_UartRead_DataLen);

                        for (i = 0; i < g_InPer_UartRead_DataLen; i++)
                        {
                            PRT_NORMAL("0x%02x ", buff[i]);
                        }
                        PRT_NORMAL("\n");

                        int temp = g_InPer_UartRead_DataLen;
                        g_InPer_UartRead_Head_Flag = 0;
                        g_InPer_UartRead_Len = 0;
                        g_InPer_UartRead_DataLen = DEF_HEAD_LENGTH;
                        memset(rec_b, 0, sizeof(rec_b));

                        usleep(10 * 1000);
                        return temp;
                    }
                }
            }
            else
            {
                if ((rec_b[0] == DEF_HEAD_FIRST) && (g_InPer_UartRead_Len==1))
                {
                    switch (temp_r)
                    {
                    case DEF_HEAD_SECOND:
                        rec_b[1] = temp_r;
                        g_InPer_UartRead_Head_Flag = 1;
                        g_InPer_UartRead_Len = 2;
                        break;

                    default:
                        rec_b[0] = 0;
                        g_InPer_UartRead_Len = 0;
                        break;
                    }
                    usleep(10 * 1000);
                }
                else
                {
                    if ((temp_r == DEF_HEAD_FIRST) && (g_InPer_UartRead_Len==0))
                    {
                        rec_b[0] = temp_r;
                        g_InPer_UartRead_Len = 1;
                    }
                    else
                    {
                        rec_b[0] = 0;
                        g_InPer_UartRead_Len = 0;
                    }
                    usleep(10 * 1000);
                }
            }
        }
    }
    else if (retval == 0)
    {
        usleep(1000);
        return 0;
    }

    return nread;
}
